public class MariSolSiteLoginController {
 private static final String USERNAME_PARAM = 'un'; 
    private static final String PASSWD_PARAM = 'pw'; 
    
    
    /*
    * Makes a login to customer portal based on request parameters. If parameters are not 
    * spesified an error message will be shown
    */
    public PageReference login() {
        //StartUrl = https://na9.salesforce.com/home/home.jsp
        String startUrl = System.currentPageReference().getParameters().get('startURL');
        System.debug('***' + startURL);
        String un = System.currentPageReference().getParameters().get(USERNAME_PARAM);
        String pw = System.currentPageReference().getParameters().get(PASSWD_PARAM);
        
        if(un == null || pw == null || un.length() == 0 || pw.length() ==0){
          System.debug(System.Logginglevel.ERROR, 'MarisolSiteLoginController: Failed to login, missing un or pw');  
          //Logger.writeMessage(Logger.Level.ERROR, 'FnSiteLoginController: login failed, missing information, un = ' + un + ' pw = ' + pw, null);
          return null;
        }
        
        Pagereference ref = Site.login(un, pw, startUrl);
       
         if(ref == null){
          //Get the inaccesible url for the current site
          //SiteSettings__c setting = SiteSettings__c.getValues(Site.getName());
          //String url = setting == null ? null : setting.Inaccessible_url__c;
          
          //System.debug('***Site ' + Site.getName() + ' inaccssessible for user ' + setting.Name);
          //Logger.writeMessage(Logger.Level.ERROR, 'FnSiteLoginController: site login failed with parameters un = ' + un + ' pw = ' + pw, null);
          return new Pagereference('www.vg.no');    
        }
        
        //Set the random password after login so that the current one cannot be used again
        /*User u = [select Id from User where Username = :un];
        System.setPassword(u.Id, PasswordGenerator.getRandomPassword(10));
        String prefix = Site.getPrefix() == null ? '' : Site.getPrefix();
        String newUrl = EncodingUtil.urlEncode(prefix + '/apex/RegisterSession?startURL=' + EncodingUtil.urlEncode(startUrl, 'UTF-8'), 'UTF-8');
        ref.getParameters().put('startURL', newUrl);
        ref.getParameters().put('retURL', newUrl);
        return ref;*/
       
        return ref;
    }
    
   /* private static testMethod void testFnSiteLoginControllerWithInvalidParams () {
        // Instantiate a new controller with all parameters in the page
        FnSiteLoginController controller = new FnSiteLoginController ();
        
        Test.startTest();
        
        PageReference testPage = new PageReference('');
        testPage.getParameters().put(USERNAME_PARAM, null);
        testPage.getParameters().put(PASSWD_PARAM, '123456');
        Test.setCurrentPageReference(testPage);
        
        //Cannot actually do the assert because the site context cannot be set in the test method
        controller.login();                          
      
      Test.stopTest();
      
    }*/
    
   /* private static testMethod void testFnSiteLoginControllerWithMissingParams () {
        // Instantiate a new controller with all parameters in the page
        FnSiteLoginController controller = new FnSiteLoginController ();
        Apexpages.currentPage().getParameters().put(USERNAME_PARAM, null);
        Apexpages.currentPage().getParameters().put(PASSWD_PARAM, '123456');
                
        System.assertEquals(controller.login(),null);                           
    } */   
}